package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 益丰大药房
 * @author tngou@tngou.net
 *
 */
public class COM_Yfdyf  extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(COM_Yfdyf.class);

	private String url="http://www.yfdyf.com/product-{}.html";
	

	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null||doc.text().indexOf("你要打开的链接找不到了")>=0)return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		String[] uns = url.toString().split("-");
		json.put("un",uns[uns.length-1].replaceAll(".html", "-").split("-")[0]);
		//标签
		Elements mbx = doc.getElementsByClass("subnav-position");
		if(mbx.first()!=null)
		{
			Elements as = mbx.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 1; i < as.size(); i++) {
				list.add(as.get(i).text());
			}				
			json.put("tag", list);
		}
		//图片
		Elements thumblist = doc.getElementsByClass("goods-detail-pic-thumbnail");
		if(thumblist.first()!=null)
		{		
			Elements imgs = thumblist.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				JSONObject rel = (JSONObject) JSON.parse(e.attr("imginfo"));
				
				if(rel!=null)
				{
					String src = rel.getString("big");
					if(StringUtil.isNotEmpty(src))
					{
						list.add(src);
					}
				}
				
			});
			json.put("picture", list);
		}
		//价格
		Elements price = doc.getElementsByClass("goodsprice");
		if(price.first()!=null)
		{	
			String value = price.first().text();
			
			json.put("price", getPrice(value));
			
		}
		//类型
		Elements buy_title = doc.getElementsByClass("tit-label");
		if(buy_title.first()!=null)
		{
			
			String s = buy_title.first().text();
			if(StringUtil.isNotEmpty(s))
			{
				if(s.toUpperCase().indexOf("RX")>=0)
				{
					json.put("ptype", Ptype.RX处方药.ordinal());
				}else if(s.toUpperCase().indexOf("OTC")>=0)
				{
					json.put("ptype", Ptype.OTC非处方甲类.ordinal());
				}
			
			}
		}
		//说明书
	
			JSONObject instructions=instructions(json.getString("un"));		
			json.put("instructions",instructions );
			
	
		return json;
	}
	private JSONObject instructions(String un) {
		JSONObject json = new JSONObject(true);
		Document doc = HttpUtil.get("http://www.yfdyf.com/product-goodsBodyContent-"+un+".html");
		if(doc==null) return json;
		String html = doc.getElementsByClass("textarea-lazyload").text();
		doc=Jsoup.parse(html);
		Elements details = doc.getElementsByClass("instruction-details");
		if(details.first()!=null)
		{
			Elements dls = details.first().getElementsByTag("dl");
			for (Element dl : dls) {
				Element dt = dl.getElementsByTag("dt").first();
				Element dd = dl.getElementsByTag("dd").first();
				if(dt!=null&&dd!=null) {
					String key=StringUtil.cleanKey(dt.text());
					if(!"药品名称".equals(key))
					json.put(key, dd.text());
				}
			}
	
		}
	   
		return  name_number(json);
	}
	
}


